package com.zhuoya.healthcard;

import android.support.annotation.RequiresApi;
import android.widget.*;

import com.tx.printlib.*;

import android.hardware.usb.*;
import android.content.*;
import android.os.*;
import android.view.*;

import java.text.*;
import java.util.*;

public class PaySuccessActivity extends BaseActity {
    private Boolean A;
    private String authCode;
    private String expressMoney;
    private TextView mTimeCount;
    private UsbPrinter mUsbPrinter;
    private String name;
    private String outTradeNo;
    private CountDownTimer timer;

    public PaySuccessActivity() {
        this.A = true;
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    private UsbDevice getCorrectDevice() {
        final UsbManager usbMgr = (UsbManager) getSystemService(Context.USB_SERVICE);
        final Map<String, UsbDevice> devMap = usbMgr.getDeviceList();
        for (String name : devMap.keySet()) {
            if (UsbPrinter.checkPrinter(devMap.get(name))) {
                final UsbDevice usbDevice = devMap.get(name);
                final String productName = usbDevice.getProductName();
                if (productName != null && " TX PRINTER ".equals(productName)) {
                    return usbDevice;
                }
                continue;
            }
        }
        return null;
    }

    private void startCount() {
        this.timer = new CountDownTimer(10000L, 1000L) {
            public void onFinish() {
                PaySuccessActivity.this.startActivity(new Intent((Context) PaySuccessActivity.this, (Class) MainActivity.class));
                PaySuccessActivity.this.finish();
            }

            public void onTick(final long n) {
                PaySuccessActivity.this.mTimeCount.setText((CharSequence) String.format(Locale.CHINA, "%ds", n / 1000L));
            }
        }.start();
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(final Bundle bundle) {
        super.onCreate(bundle);
        this.setContentView(R.layout.activity_pay_success);
        final Intent intent = this.getIntent();
        this.outTradeNo = intent.getStringExtra("outTradeNo");
        this.authCode = intent.getStringExtra("authCode");
        this.name = intent.getStringExtra("name");
        this.expressMoney = intent.getStringExtra("expressMoney");
        this.mUsbPrinter = new UsbPrinter(this.getApplicationContext());
        this.mTimeCount = this.findViewById(R.id.timeCount);
        this.startCount();
        this.findViewById(R.id.backTV).setOnClickListener(new View.OnClickListener() {
            public void onClick(final View view) {
                try {
                    PaySuccessActivity.this.startActivity(new Intent((Context) PaySuccessActivity.this, (Class) MainActivity.class));
                    PaySuccessActivity.this.finish();
                } catch (Exception ex) {
                }
            }
        });
        this.findViewById(R.id.back).setOnClickListener(new View.OnClickListener() {
            public void onClick(final View view) {
                try {
                    PaySuccessActivity.this.startActivity(new Intent((Context) PaySuccessActivity.this, (Class) MainActivity.class));
                    PaySuccessActivity.this.finish();
                } catch (Exception ex) {
                }
            }
        });
        this.print();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        this.A = false;
        final CountDownTimer timer = this.timer;
        if (timer != null) {
            timer.cancel();
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public String print() {
        final UsbDevice correctDevice = this.getCorrectDevice();
        if (correctDevice == null || !this.mUsbPrinter.open(correctDevice)) {
            return "null";
        }
        this.mUsbPrinter.init();
        final String format = String.format("%04XH", this.mUsbPrinter.getStatus2());
        if ("8018H".equals(format)) {
            this.mUsbPrinter.doFunction(7, 1, 0);
            this.mUsbPrinter.doFunction(1, 1, 1);
            this.mUsbPrinter.doFunction(11, 1, 0);
            this.mUsbPrinter.doFunction(13, 0, 0);
            this.mUsbPrinter.outputStringLn("健康证办理[快递缴费凭条]");
            this.mUsbPrinter.newline();
            this.mUsbPrinter.resetFont();
            this.mUsbPrinter.doFunction(1, 0, 1);
            this.mUsbPrinter.doFunction(11, 1, 0);
            this.mUsbPrinter.doFunction(13, 5, 0);
            final UsbPrinter mUsbPrinter = this.mUsbPrinter;
            final StringBuilder sb = new StringBuilder();
            sb.append("姓    名：");
            sb.append(this.name);
            mUsbPrinter.outputStringLn(sb.toString());
            this.mUsbPrinter.newline();
            this.mUsbPrinter.doFunction(7, 1, 0);
            this.mUsbPrinter.doFunction(1, 0, 1);
            this.mUsbPrinter.doFunction(11, 1, 0);
            this.mUsbPrinter.doFunction(13, 5, 0);
            if ("28".equals(this.authCode.substring(0, 2))) {
                this.mUsbPrinter.outputStringLn("支付类型：支付宝");
            } else {
                this.mUsbPrinter.outputStringLn("支付类型：微信支付");
            }
            this.mUsbPrinter.doFunction(7, 1, 0);
            this.mUsbPrinter.newline();
            this.mUsbPrinter.doFunction(1, 0, 1);
            this.mUsbPrinter.doFunction(11, 1, 0);
            this.mUsbPrinter.doFunction(13, 5, 0);
            this.mUsbPrinter.outputStringLn("订单编号：" + this.outTradeNo);
            this.mUsbPrinter.newline();
            this.mUsbPrinter.doFunction(1, 0, 1);
            this.mUsbPrinter.doFunction(11, 1, 0);
            this.mUsbPrinter.doFunction(13, 5, 0);
            final UsbPrinter mUsbPrinter2 = this.mUsbPrinter;
            final StringBuilder sb2 = new StringBuilder();
            sb2.append("支付金额：￥");
            sb2.append(this.expressMoney);
            mUsbPrinter2.outputStringLn(sb2.toString());
            this.mUsbPrinter.newline();
            this.mUsbPrinter.doFunction(1, 0, 1);
            this.mUsbPrinter.doFunction(11, 1, 0);
            this.mUsbPrinter.doFunction(13, 5, 0);
            final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            final UsbPrinter mUsbPrinter3 = this.mUsbPrinter;
            final StringBuilder sb3 = new StringBuilder();
            sb3.append("支付时间：");
            sb3.append(simpleDateFormat.format(new Date()));
            mUsbPrinter3.outputStringLn(sb3.toString());
            this.mUsbPrinter.newline();
            this.mUsbPrinter.doFunction(1, 0, 1);
            this.mUsbPrinter.doFunction(11, 1, 0);
            this.mUsbPrinter.doFunction(13, 0, 0);
            this.mUsbPrinter.outputStringLn("备注：若体检结果不合格，系统将自动进行退款");
            this.mUsbPrinter.newline();
            this.mUsbPrinter.doFunction(12, 0, 0);
            this.mUsbPrinter.close();
            return format;
        }
        return format;
    }
}
